//时间戳(秒)转时分秒 
function formatTime(time) {
  if (typeof time !== 'number' || time < 0) {
    return time
  }

  var hour = parseInt(time / 3600)
  time = time % 3600
  var minute = parseInt(time / 60)
  time = parseInt(time % 60)
  var second = time

  return ([hour, minute, second]).map(function (n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }).join(':')
}

function formatLocation(longitude, latitude) {
  if (typeof longitude === 'string' && typeof latitude === 'string') {
    longitude = parseFloat(longitude)
    latitude = parseFloat(latitude)
  }

  longitude = longitude.toFixed(2)
  latitude = latitude.toFixed(2)

  return {
    longitude: longitude.toString().split('.'),
    latitude: latitude.toString().split('.')
  }
}
//获取当前：年月日时分秒
function curTime() {
  let date = new Date();
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hour = date.getHours()
  let minute = date.getMinutes()
  let second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')

}

//当前时间的时分判断是否在一个时间区间内    var flag= checkAuditTime("11:30","20:30");
function checkAuditTime(beginTime,endTime){
  if(!beginTime || !endTime){
    return 0;
  }
  var nowDate=new Date();
  var beginDate=new Date(nowDate);
  var endDate=new Date(nowDate);
  
  var beginIndex=beginTime.lastIndexOf("\:");
  var beginHour=beginTime.substring(0,beginIndex);
  var beginMinue=beginTime.substring(beginIndex+1,beginTime.length);
  beginDate.setHours(beginHour,beginMinue, 0, 0);
  
  var endIndex=endTime.lastIndexOf("\:");
  var endHour=endTime.substring(0,endIndex);
  var endMinue=endTime.substring(endIndex+1,endTime.length);
  endDate.setHours(endHour, endMinue, 0, 0);
  if(nowDate.getTime()-beginDate.getTime()>=0 && nowDate.getTime() <= endDate.getTime()){
      return 1;    
  }else{
      return 0;
  }
}

//获取当前： 年月日
function curRiqi() {
  let date = new Date();
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  // if (month < 10){
  //   month = '0' + month
  // }
  let day = date.getDate()
  // if (day<10){
  //   day = '0' + day
  // }
  return [year, month, day]
}

//时间戳（毫秒） 转 时间
const formatH = (timestamp, more = true) => {
  var date = new Date(timestamp)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  //年月日 时分秒
  if (more) {
    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  } else {
    //年月日  
    return [year, month, day].map(formatNumber).join('-')
  }

}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//日期时间  转  时间戳(毫秒)
function riqiTime(riqi) {
  var str = riqi.replace(/-/g, '/');
  return new Date(str).getTime();
}


/*获取随机数*/
function getRandom(min, max) {
  return Math.floor(Math.random() * (max - min)) + min + 1;
}

/* 常用正则表达式集 */
const regExps = {
  email: /^[0-9a-zA-Z_]+@[0-9a-zA-Z_]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$/, //邮箱
  mobile: /^(?:1\d{2})-?\d{5}(\d{3}|\*{3})$/, //手机号码
  qq: /^[1-9][0-9]{4,9}$/, //QQ
  befitName: /^[a-z0-9A-Z\u4e00-\u9fa5]+$/, //合适的用户名，中文,字母,数字
  befitPwd: /^[a-z0-9A-Z_]+$/, //合适的用户名，字母,数字,下划线
  allNumber: /^[0-9]+.?[0-9]$/ //全部为数字
}

/* 分享设置 */
let shareSet = (that,title="")=>{

  if(!title){
    title = that.data.pageTitle;
  }
  var dangqiInfo =  "";
  if(wx.getStorageSync('dangqiInfo')){
    dangqiInfo =  encodeURIComponent(JSON.stringify(wx.getStorageSync('dangqiInfo')));
  }
  var hall_info = "";
  if(wx.getStorageSync('hall_info')){
    hall_info = encodeURIComponent(JSON.stringify(wx.getStorageSync('hall_info')));
  }
  console.log("/page/wheat/pages/share/share?shareUrl="+ that.route +"&dangqiInfo="+dangqiInfo+"&hall_info="+hall_info,"分享路径");
  return {
      title: title,
      path: "/page/wheat/pages/share/share?shareUrl="+ that.route +"&dangqiInfo="+dangqiInfo+"&hall_info="+hall_info,
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
  };
}

module.exports = {
  formatTime: formatTime,
  formatLocation: formatLocation,
  curTime,
  checkAuditTime,
  curRiqi,
  formatH,
  riqiTime,
  getRandom,
  regExps,
  shareSet
}